﻿using System;
using System.Linq;
using FarmerJoe.Lists;
using Styx;
using Styx.CommonBot;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;

namespace FarmerJoe.Helpers
{
    static class Extensions
    {
        public static string SafeName(this WoWObject obj)
        {
            if (obj.IsMe) { return "Me"; }

            string name;
            if (obj is WoWPlayer)
            {
                if (RaFHelper.Leader == obj)return "Tank";
                name = ((WoWPlayer)obj).Class.ToString();
            }
            else if (obj is WoWUnit && obj.ToUnit().IsPet){name = "Pet";}
            else{name = obj.Name;}

            return name;
        }

        public static bool IsWanding(this LocalPlayer me)
        {
            return StyxWoW.Me.AutoRepeatingSpellId == 5019;
        }

        public static int AuraTimeLeft(this WoWUnit onUnit, string auraName)
        {
            WoWAura wantedAura = onUnit.GetAllAuras().FirstOrDefault(a => a.Name == auraName && a.CreatorGuid == StyxWoW.Me.Guid);

            return wantedAura != null ? wantedAura.TimeLeft.Seconds : 0;
        }

        public static TimeSpan AuraTimeLeft(this WoWUnit onUnit, string auraName, bool fromMyAura)
        {
            WoWAura wantedAura = onUnit.GetAllAuras().FirstOrDefault(a => a.Name == auraName && (!fromMyAura || a.CreatorGuid == StyxWoW.Me.Guid));

            return wantedAura != null ? wantedAura.TimeLeft : TimeSpan.Zero;
        }

        public static TimeSpan AuraTimeLeft(this WoWUnit onUnit, int auraID, bool fromMyAura)
        {
            WoWAura wantedAura = onUnit.GetAllAuras().FirstOrDefault(a => a.SpellId == auraID && (!fromMyAura || a.CreatorGuid == StyxWoW.Me.Guid));

            return wantedAura != null ? wantedAura.TimeLeft : TimeSpan.Zero;
        }

        public static bool ValidUnit(WoWUnit p)
        {
            if (NPCs.IgnoreMobs.Contains(p.Entry)) return false;

            // Ignore shit we can't select/attack
            if (!p.CanSelect || !p.Attackable) return false;  

            // Ignore friendlies!
            if (p.IsFriendly)return false;

            // Duh
            if (p.IsDead)return false;

            // Dummies/bosses are valid by default. Period.
            if (p.IsTrainingDummy() || p.IsBoss())return true;

            // If its a pet, lets ignore it please.
            if (p.IsPet || p.OwnedByRoot != null)return false;

            // And ignore critters/non-combat pets
            if (p.IsNonCombatPet || p.IsCritter)return false;

            return true;
        }

        public static bool IsTrainingDummy(this WoWUnit unit)
        {
            return NPCs.TrainingDummies.Contains(unit.Entry);
        }
        
        
        public static bool IsBoss(this WoWUnit unit)
        {
            return NPCs.IsBoss(unit);
        }

        #region Has Aura

        
        public static bool HasAura(this WoWUnit unit, string aura)
        {
            return HasAura(unit, aura, 0);
        }

        public static bool HasAura(this WoWUnit unit, string aura, int stacks)
        {
            return HasAura(unit, aura, stacks, null);
        }
/*
        public static bool HasMyAura(this WoWUnit unit, string aura)
        {
            return HasMyAura(unit, aura, 0);
        }

//        public static bool HasMyAura(this WoWUnit unit, string aura, int stacks)
//        {
//            return HasAura(unit, aura, stacks, StyxWoW.Me);
//        }
*/
        private static bool HasAura(this WoWUnit unit, string aura, int stacks, WoWUnit creator)
        {
            return unit.GetAllAuras().Any(a => a.Name == aura && a.StackCount >= stacks && (creator == null || a.CreatorGuid == creator.Guid));
        }
        #endregion
    }
}
